//
//  BIDSaleHouseListViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/16.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDSaleHouseListViewController.h"
#import "BIDPublishSaleHouseInfoViewController.h"
#import "BIDSaleHouseDetailViewController.h"
#import "BIDSaleHouseCell.h"

#import <SDWebImage/UIImageView+WebCache.h>

static NSString *saleHouseListURL = @"saleAndroid/list.action";

@interface BIDSaleHouseListViewController ()<BIDBaseListViewDelegate>
{
    BOOL _isRegister;
}
@end

@implementation BIDSaleHouseListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"售房";
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(publishRentInfo)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.curPage = 0;
    [self.dataSourceArr removeAllObjects];
    [self loadData];
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], saleHouseListURL];
    NSMutableString *strPost = [[NSMutableString alloc] initWithFormat:@"page=%ld&rows=%ld", self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            NSArray *arr = responseDic[@"rows"];
            if(arr.count > 0)
            {
                [self.dataSourceArr addObjectsFromArray:arr];
                if(self.dataSourceArr.count < self.curPage*self.countPerPage)
                {
                    [self.myTableView hasNoMoreData];
                }
                else
                {
                    [self.myTableView hasMoreData];
                }
            }
            [self.myTableView reloadData];
        }
        [self autoDecideToShowEmptyView];
    }];
}

/**
 发布卖房信息
 */
- (void)publishRentInfo
{
    BIDPublishSaleHouseInfoViewController *vc = [[BIDPublishSaleHouseInfoViewController alloc] initWithNibName:@"BIDPublishSaleHouseInfoViewController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self setNavigationControllerAttribute:nav];
    [self presentViewController:nav animated:YES completion:^{}];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;

    if(!_isRegister)
    {
        _isRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDSaleHouseCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDSaleHouseCell *saleHouseCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = self.dataSourceArr[row];
    NSArray *fileArr = dic[@"fileList"];
    if(fileArr && fileArr.count>0)
    {
        NSDictionary *fileDic = fileArr[0];
        NSString *strFilePath = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], fileDic[@"path"]];
        [saleHouseCell.imgView setImageWithURL:[NSURL URLWithString:strFilePath] placeholderImage:[UIImage imageNamed:@"pic_placeholder.png"]];
    }
    else
    {
        [saleHouseCell.imgView setImage:[UIImage imageNamed:PIC_PLACEHOLDER]];
    }
    saleHouseCell.titleLabel.text = [[NSString alloc] initWithFormat:@"%@\r ", dic[@"content"]];
    saleHouseCell.rentTypeLabel.text = [[NSString alloc] initWithFormat:@"%@ %@㎡", dic[@"houseTypeName"], dic[@"area"]];
    NSString *strPrice = ANY2NSSTRING(dic[@"price"]);
    saleHouseCell.priceLabel.text = [[NSString alloc] initWithFormat:@"%@万", strPrice];
    cell = saleHouseCell;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSUInteger row = indexPath.row;
    NSDictionary *dic = self.dataSourceArr[row];
    BIDSaleHouseDetailViewController *vc = [[BIDSaleHouseDetailViewController alloc] initWithNibName:@"BIDSaleHouseDetailViewController" bundle:nil];
    vc.saleID = dic[@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 109.f;
}


@end
